// pages/edit/edit.js
const app = getApp();
const ipUrl = app.globalData.ipUrl;
import {
    updateUserinfo
} from "../../apis/uerInfo"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        username: '',
        nickname: '',
        profile: '',
        phone: '',
        email: '',
        gender: '',
        birthday: '',
        avatar: '',
        sexText: '',
        sexValue: [],
        sexs: [{
                label: '男',
                value: 'male'
            },
            {
                label: '女',
                value: 'female'
            },
            {
                label: '未知',
                value: 'unknown'
            },
        ],
        mode: '',
        dateVisible: false,
        date: new Date('2021-12-23').getTime(), // 支持时间戳传入
        dateText: '',
        // 指定选择区间起始值
        start: '2000-01-01 00:00:00',
        end: '2024-07-24 12:12:12',

    },
    handleUserinfo: function () {
        console.log(this.data)
        wx.showLoading({
            title: '修改提交中',
          })
          
        updateUserinfo(this.data).then((res) => {
              setTimeout(function () {
                wx.navigateBack();
                wx.hideLoading()
                wx.showToast({
                    title: '修改成功',
                    icon: 'success',
                    duration: 1000
                  })
                  
              }, 1000)
            this.setData({
                nickname: res.nickname,
                profile: res.profile,
                phone: res.phone,
                email: res.email,
                gender: res.gender,
                sexText: res.gender == 'male' ? "男" : res.gender == 'female' ? "女" : "位置",
                birthday: res.birthday,
                dateText: res.birthday,
                avatar: ipUrl+res.avatar
            })
            this.data.userInfo.nickname= res.nickname,
            this.data.userInfo.profile=res.profile,
            this.data.userInfo.phone=res.phone,
            this.data.userInfo.email= res.email,
            this.data.userInfo.gender= res.gender,
            this.data.userInfo.birthday= res.birthday,
            this.data.userInfo.dateText= res.birthday,
            this.data.userInfo.avatar=res.avatar
            wx.setStorageSync('userInfo', this.data.userInfo);
            
        }).catch((res) => {
            setTimeout(function () {
                wx.hideLoading()
                wx.showToast({
                    title: '修改失败请重试',
                    icon: 'error',
                    duration: 1000
                  })
              }, 1000)
          });
    },
    getUserStorage() {
        this.setData({
            username: this.data.userInfo.user,
            nickname: this.data.userInfo.nickname,
            profile: this.data.userInfo.profile,
            phone: this.data.userInfo.phone,
            email:this.data.userInfo.email,
            gender: this.data.userInfo.gender,
            birthday: this.data.userInfo.birthday,
            dateText:this.data.userInfo.birthday,
            avatar: ipUrl+this.data.userInfo.avatar,
            sexText: this.data.userInfo.gender == 'male' ? "男" : res.gender == 'female' ? "女" : "位置",
        })
    },
    inputData(e) {
        const flag = e.target.dataset.flag
        this.setData({
            [`${e.target.dataset.flag}`]: e.detail.value
        })
        console.log(this.data)
    },
    showPicker(e) {
        const {
            mode
        } = e.currentTarget.dataset;
        this.setData({
            mode,
            [`${mode}Visible`]: true,
        });
    },
    hidePicker() {
        const {
            mode
        } = this.data;
        this.setData({
            [`${mode}Visible`]: false,
        });
    },
    onConfirm(e) {
        const {
            value
        } = e.detail;
        const {
            mode
        } = this.data;
        this.setData({
            birthday: value
        })
        console.log('confirm', value);

        this.setData({
            [mode]: value,
            [`${mode}Text`]: value,
        });

        this.hidePicker();
    },

    onColumnChange(e) {
        console.log('pick', e.detail.value);
    },


    onColumnChange(e) {
        console.log('picker pick:', e);
    },

    onPickerChange(e) {
        const {
            key
        } = e.currentTarget.dataset;
        const {
            value
        } = e.detail;
        const {
            label
        } = e.detail;

        console.log('picker change:', e.detail);
        this.setData({
            [`${key}Visible`]: false,
            [`${key}Value`]: value,
            [`${key}Text`]: label.join(' '),
            gender: value[0]
        });
        console.log(this.data)
    },

    onPickerCancel(e) {
        const {
            key
        } = e.currentTarget.dataset;
        console.log(e, '取消');
        console.log('picker1 cancel:');
        this.setData({
            [`${key}Visible`]: false,
        });
    },

    onsexPicker() {
        this.setData({
            sexVisible: true
        });
    },



    bindchooseavatar(e) {
        console.log("avatarUrl", e.detail.avatarUrl)
        this.setData({
            avatar: e.detail.avatarUrl
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            userInfo: wx.getStorageSync('userInfo')
        })
        this.getUserStorage();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})